<template>
	<view class="mobile-item-container">
    <Navbar title="发表动态" bgColor="#fff" :h5Show="true"></Navbar>
		<u--form labelPosition="left"
						:model="form"
						ref="uForm"
				>
					<u-form-item prop="content">
					<u--textarea v-model="form.content" maxlength="-1" count autoHeight placeholder="说点什么吧~" confirm-type="send"></u--textarea>
					</u-form-item>
					<u-form-item>
						<u-upload
								:fileList="fileList"
								@afterRead="afterRead"
								@delete="deletePic"
								multiple
								:maxCount="10"
							></u-upload>
					</u-form-item>
				</u--form>
			<u-row :gutter="20" style="margin-top: 36px;">
					  <u-col :span="6">
						<u-button type="success" @click="submit">提交</u-button>
					  </u-col>
					  <u-col :span="6">
						<u-button icon="arrow-left" text="返回" plain @click="goBack()"></u-button>
					  </u-col>
			</u-row>
	</view>
</template>

<script>
	import * as communityApi from "@/api/business/community"
	
	import Navbar from '@/components/navbar/Navbar'
	
	const { environment } = require('@/config/environment.js')
	import { ACCESS_TOKEN } from '@/store/mutation-types'
	import storage from '@/utils/storage';
	
	export default {
    components:{Navbar},
		data() {
			return {
				id: undefined,
				form: {
					content: undefined,
					picPath: undefined,
					parentId: 0,
					releaseTime: uni.$u.timeFormat(new Date().getTime(), 'yyyy-mm-dd hh:MM:ss')
				},
				fileList: [],
				picList: [] 
			}
		},
		onLoad() {
			this.getInfo ()
		},
		methods: {
			getInfo () {
			  const app = this
			  app.$store.dispatch('Info').then(res => {
			    app.form.comeForm = res.nickName
			  })
			},
			submit(){
				this.form.picPath = this.picList.toString()
				communityApi.communityAdd(this.form).then(res => {
					uni.showToast({ title: res.msg })
					this.goBack ()
				})
			},
			goBack () {
				uni.navigateBack({
				    delta: 1, // 返回层数，2则上上页
				    success() {
				        uni.$emit('update',{msg:'页面更新'})
				    }
				})		
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.$delete(this.picList, event.index)
			},			
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},			
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: environment.baseURL + "/common/uploads", // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'files',
						formData: {},
						header: {
						  "Authorization": "Bearer " + storage.get(ACCESS_TOKEN)
						},
						success: (res) => {
							const that = this
							let data = JSON.parse(res.data)
							setTimeout(() => {
								resolve(data.urls)
								that.picList.push(data.fileNames)
							}, 1000)
						}
					});
				})
			},	
			
		}
	}
</script>

<style>
</style>